package pers.lt.admin.dao;

import pers.lt.hibernatebeans.*;
import pers.lt.tools.BeanUtil;
import pers.lt.tools.StringFacs;
import pers.lt.jdbc.JdbcTemplate;
import pers.lt.jdbc.Page;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by 18621 on 2016/7/5.
 */
@Component("AdminDao")
public class AdminDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    //查询用户
    public UserInfoEntity userSelect(UserInfoEntity userInfoEntity){
        return jdbcTemplate.get(UserInfoEntity.class, userInfoEntity.getId());
    }
    //保存用户信息,通过以下方法可以实现保存更新一体化
    public UserInfoEntity userSave(UserInfoEntity userInfoEntity){
        UserInfoEntity a=new UserInfoEntity();
        a=jdbcTemplate.get(UserInfoEntity.class, userInfoEntity.getId());
        if(StringFacs.isEmpty(a)){
            a=userInfoEntity;
        }else{
            BeanUtil.copyProperties(userInfoEntity, a);
        }
        jdbcTemplate.saveOrUpdate(a);
        return a;
    }
    //保存用户信息,通过以下方法可以实现保存更新一体化
    public void userDelete(UserInfoEntity userInfoEntity){
        jdbcTemplate.delete(userInfoEntity);
    }

    //这种原生写法 用户登录检查
    public UserInfoEntity queryUser(UserInfoEntity userInfoEntity){
        StringBuffer sql = new StringBuffer();
        sql.append("select * from USER_INFO where 1=1 ");
        sql.append(" and  USERNAME='").append(userInfoEntity.getUsername()).append("'");
        sql.append(" and PASSWORD='").append(userInfoEntity.getPassword()).append("'");
        List list = jdbcTemplate.queryEntitys( sql.toString(), UserInfoEntity.class);
        if(list.size()>0) return (UserInfoEntity)list.get(0);
        return null;
    }
    //用户列表
    public Page userList(UserInfoEntity userInfoEntity,Page page){
        StringBuffer sql = new StringBuffer();

        sql.append(" select * from user_info t where 1=1 ");
        if(!StringFacs.isEmpty(userInfoEntity.getUsername()))  sql.append(" and username like '%").append(userInfoEntity.getUsername()).append("%'");
        if(!StringFacs.isEmpty(userInfoEntity.getOfficesid()))  sql.append(" and officesid = '").append(userInfoEntity.getOfficesid()).append("'");
        return jdbcTemplate.pageList(sql.toString(),page);
    }
    //查询角色
    public RoleInfoEntity roleSelect(RoleInfoEntity roleInfoEntity){
        RoleInfoEntity a=new RoleInfoEntity();
        a=jdbcTemplate.get(RoleInfoEntity.class, roleInfoEntity.getId());
        return a;
    }
    //保存角色
    public RoleInfoEntity addRole(RoleInfoEntity roleInfoEntity){
        RoleInfoEntity a=new RoleInfoEntity();
        a=jdbcTemplate.get(RoleInfoEntity.class, roleInfoEntity.getId());
        if(StringFacs.isEmpty(a)){
            a=roleInfoEntity;
        }else{
            BeanUtil.copyProperties(roleInfoEntity, a);
        }
        jdbcTemplate.saveOrUpdate(a);
        return a;
    }

    public void roleDelete(RoleInfoEntity roleInfoEntity){
        jdbcTemplate.delete(roleInfoEntity);
    }
    //角色列表
    public Page roleList(RoleInfoEntity roleInfoEntity,Page page){
        StringBuffer sql = new StringBuffer();
        sql.append("select * from role_info t where 1=1 ");
        if(!StringFacs.isEmpty(roleInfoEntity.getRolename()))  sql.append(" and rolename like '%").append(roleInfoEntity.getRolename()).append("%'");
        return jdbcTemplate.pageList(sql.toString(),page);
    }
    //查询权限
    public RightsInfoEntity selectRights(RightsInfoEntity rightsInfoEntity){
        RightsInfoEntity a=new RightsInfoEntity();
        a=jdbcTemplate.get(RightsInfoEntity.class, rightsInfoEntity.getId());
        return a;
    }
    //保存权限
    public RightsInfoEntity addRights(RightsInfoEntity rightsInfoEntity){
        RightsInfoEntity a=new RightsInfoEntity();
        a=jdbcTemplate.get(RightsInfoEntity.class, rightsInfoEntity.getId());
        if(StringFacs.isEmpty(a)){
            a=rightsInfoEntity;
        }else{
            BeanUtil.copyProperties(rightsInfoEntity, a);
        }
        jdbcTemplate.saveOrUpdate(a);
        return a;
    }
    public void rightsDelete(RightsInfoEntity rightsInfoEntity){
        jdbcTemplate.delete(rightsInfoEntity);
    }
    //权限列表
    public List<Map> rightsList(RightsInfoEntity rightsInfoEntity){
        StringBuffer sql = new StringBuffer();
        //select t.* ,（select count(*)  from rights_info where pid = t.id）children from rights_info t
        //查出权限，及权限的子权限个数
        //select t.* ,aaa.zs child from  rights_info t left join (select pid,count(*) zs from rights_info group by  pid) aaa on aaa.pid = t.id
        sql.append("select t.* ,aaa.zs child from  rights_info t left join (select pid,count(*) zs from rights_info group by  pid) aaa on aaa.pid = t.id where 1=1 ");
        //sql.append("select t.* ,（select count(*)  from rights_info where pid = t.id）child from rights_info t where 1=1 ");
        if(!StringFacs.isEmpty(rightsInfoEntity.getPid())) sql.append(" and t.pid = '").append(rightsInfoEntity.getPid()).append("'");
        //如果Pid为空，则查出权限的最终父节点
        else sql.append(" and t.pid = '' ");
        if(!StringFacs.isEmpty(rightsInfoEntity.getRightName()))  sql.append(" and t.right_name like '%").append(rightsInfoEntity.getRightName()).append("%'");
        return jdbcTemplate.queryDatas(sql.toString());
    }
    //保存用户角色对应信息,通过以下方法可以实现保存更新一体化
    public RoleUserInfoEntity userRoleSave(RoleUserInfoEntity roleUserInfoEntity){
        RoleUserInfoEntity a=new RoleUserInfoEntity();
        a=jdbcTemplate.get(RoleUserInfoEntity.class, roleUserInfoEntity.getId());
        if(StringFacs.isEmpty(a)){
            a=roleUserInfoEntity;
        }else{
            BeanUtil.copyProperties(roleUserInfoEntity, a);
        }
        jdbcTemplate.saveOrUpdate(a);
        return a;
    }
    //删除用户角色对应信息,通过传入用户的ID
    public boolean userRoleDelete(String paUserId) throws SQLException {
        return jdbcTemplate.excuteSql("delete from ROLE_USER_INFO where USER_ID='"+paUserId+"'");
    }
    //查用户有的角色列表
    public Page userRoleList(RoleUserInfoEntity roleUserInfoEntity,Page page){
        StringBuffer sql = new StringBuffer();
        sql.append(" select  t1.* , t2.user_id  from   role_info  t1  left join role_user_info t2 on t1.id = t2.role_id and user_id='").append(roleUserInfoEntity.getUserId()).append("' ");
        return jdbcTemplate.pageList(sql.toString(),page);
    }
    //删除角色权限信息,通过传入角色的ID
    public boolean roleRightsDelete(String paRoleId) throws SQLException {
        return jdbcTemplate.excuteSql("delete from role_rights_info where role_id='"+paRoleId+"'");
    }
    //保存角色权限信息,通过以下方法可以实现保存更新一体化
    public RoleRightsInfoEntity roleRightsSave(RoleRightsInfoEntity roleRightsInfoEntity){
        RoleRightsInfoEntity a=new RoleRightsInfoEntity();
        a=jdbcTemplate.get(RoleRightsInfoEntity.class, roleRightsInfoEntity.getId());
        if(StringFacs.isEmpty(a)){
            a=roleRightsInfoEntity;
        }else{
            BeanUtil.copyProperties(roleRightsInfoEntity, a);
        }
        jdbcTemplate.saveOrUpdate(a);
        return a;
    }
    //角色所有的权限列表
    public List<Map> roleRightsList(RoleRightsInfoDto roleRightsInfoDto){
        StringBuffer sql = new StringBuffer();
        //"select t.* ,（select count(*)  from rights_info where pid = t.id）children from rights_info t"
        //sql语句的意思是：查询出角色对应出的权限列表，并判断 这个权限有没有子权限。如果有子权限前台需要显示一个树节点。
        //select t.* ,aaa.zs child from  rights_info t left join (select pid,count(*) zs from rights_info group by  pid) aaa on aaa.pid = t.id where 1=1
        //sql.append(" select  t1.* , t2.role_id  ,（select count(*)  from rights_info where pid = t1.id）children  from   rights_info  t1  left join role_rights_info t2 on t1.id = t2.right_id");
        sql.append("select  t1.* , t2.role_id , aaa.zs children from   rights_info  t1 left join (select pid,count(*) zs from rights_info group by  pid) aaa on aaa.pid = t1.id   left join role_rights_info t2 on t1.id = t2.right_id");
        if(!StringFacs.isEmpty(roleRightsInfoDto.getRoleId())) sql.append(" and t2.role_id = '").append(roleRightsInfoDto.getRoleId()).append("' ");
        if(!StringFacs.isEmpty(roleRightsInfoDto.getPid())) sql.append(" where  t1.pid = '").append(roleRightsInfoDto.getPid()).append("' ");
        return jdbcTemplate.queryDatas(sql.toString());
    }
    //登录成功后查询
}
